package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.MyCommentBean;
import com.example.administrator.liveapp.utils.TimeChangeUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class CommentTab_1RecyclerViewAdapter extends RecyclerView.Adapter<CommentTab_1RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {

    public Context context;
    public List<MyCommentBean.MyVideoCommentBean> myVideoComment;

    public CommentTab_1RecyclerViewAdapter(List<MyCommentBean.MyVideoCommentBean> myVideoComment) {
        this.myVideoComment = myVideoComment;
    }

    @Override
    public CommentTab_1RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_tab_recy_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        context = parent.getContext();
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CommentTab_1RecyclerViewAdapter.ViewHolder holder, int position) {
        if (myVideoComment.get(position).getCommenttime() != null) {
            holder.articleTitle.setVisibility(View.GONE);
            holder.videoTitle.setVisibility(View.VISIBLE);
            holder.videoTitle.setText(myVideoComment.get(position).getVideoTitle());
            holder.comment_content.setText(myVideoComment.get(position).getCommentcontent());
            String time = TimeChangeUtil.getStrTime2(myVideoComment.get(position).getCommenttime());
            holder.comment_time.setText(time);
            //将position保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(position);
        }
    }

    @Override
    public int getItemCount() {
        if (myVideoComment == null) {
            return 0;
        } else {
            return myVideoComment.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView videoTitle;
        public TextView comment_content;
        public TextView comment_time;
        public TextView articleTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            videoTitle = (TextView) itemView.findViewById(R.id.videoTitle);
            comment_content = (TextView) itemView.findViewById(R.id.comment_content);
            comment_time = (TextView) itemView.findViewById(R.id.comment_time);
            articleTitle = (TextView) itemView.findViewById(R.id.articleTitle);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    private CommentTab_1RecyclerViewAdapter.OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(CommentTab_1RecyclerViewAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
